package com.silence.springboot.configurations;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.spring.boot.ConfigurationCustomizer;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import com.silence.springboot.base.BaseEntity;
import com.silence.springboot.listeners.MybatisFlexInsertListener;
import com.silence.springboot.listeners.MybatisFlexUpdateListener;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.context.annotation.Configuration;

/**
 * @author silence
 * @email 792245022@qq.com
 * @create 2023-09-20 10:02
 */
@Configuration
@RequiredArgsConstructor
public class MybatisFlexConfiguration implements ConfigurationCustomizer, MyBatisFlexCustomizer {

    /**
     * 新增监听器
     */
    private final MybatisFlexInsertListener insertListener;

    /**
     * 更新监听器
     */
    private final MybatisFlexUpdateListener updateListener;

    /**
     * 设置 mybatis-flex 全局配置
     *
     * @param globalConfig mybatis-flex 全局配置
     */
    @Override
    public void customize(FlexGlobalConfig globalConfig) {
        // 设置字段填充
        FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig();
        defaultConfig.registerInsertListener(insertListener, BaseEntity.class);
        defaultConfig.registerUpdateListener(updateListener, BaseEntity.class);
    }

    /**
     * 设置 mybatis-flex 配置日志输出
     *
     * @param configuration Configuration
     */
    @Override
    public void customize(FlexConfiguration configuration) {
        // 设置日志输出
        configuration.setLogImpl(StdOutImpl.class);
    }
}
